/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.dto.sqlServer.ReservoirArea;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.ReservoirAreaRepository;
import me.zhengjie.service.ReservoirAreaService;
import me.zhengjie.service.dto.ReservoirAreaDto;
import me.zhengjie.service.dto.ReservoirAreaQueryCriteria;
import me.zhengjie.service.mapper.ReservoirAreaMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author zhangYY
* @date 2024-01-12
**/
@Service
@RequiredArgsConstructor
public class ReservoirAreaServiceImpl implements ReservoirAreaService {

    private final ReservoirAreaRepository reservoirAreaRepository;
    private final ReservoirAreaMapper reservoirAreaMapper;

    @Override
    public PageResult<ReservoirAreaDto> queryAll(ReservoirAreaQueryCriteria criteria, Pageable pageable){
        Page<ReservoirArea> page = reservoirAreaRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(reservoirAreaMapper::toDto));
    }

    @Override
    public List<ReservoirAreaDto> queryAll(ReservoirAreaQueryCriteria criteria){
        return reservoirAreaMapper.toDto(reservoirAreaRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ReservoirAreaDto findById(Integer id) {
        ReservoirArea reservoirArea = reservoirAreaRepository.findById(id).orElseGet(ReservoirArea::new);
        ValidationUtil.isNull(reservoirArea.getId(),"ReservoirArea","id",id);
        return reservoirAreaMapper.toDto(reservoirArea);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ReservoirArea resources) {
        reservoirAreaRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ReservoirArea resources) {
        ReservoirArea reservoirArea = reservoirAreaRepository.findById(resources.getId()).orElseGet(ReservoirArea::new);
        ValidationUtil.isNull( reservoirArea.getId(),"ReservoirArea","id",resources.getId());
        reservoirArea.copy(resources);
        reservoirAreaRepository.save(reservoirArea);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            reservoirAreaRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ReservoirAreaDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ReservoirAreaDto reservoirArea : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("库区编码", reservoirArea.getAreaCode());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}